#include "Scuba.h"
#include "Tokenizer.h"

#include "inGlobal.h"

#include<string>
#include<iostream>

using namespace std;

Scuba::Scuba(void)
{
    Scope *pxScope = new InGlobal(NULL);
    m_apxScope.push_back(pxScope);
    m_pxCurrentScope=pxScope;

}

Scuba::~Scuba(void)
{
}


void Scuba::enterScope(Scope* inScope)
{
    Scope* pxOldScope = m_pxCurrentScope;

    m_apxScope.push_back(inScope);
    m_pxCurrentScope->m_asWorkspace.push_back("");
    m_pxCurrentScope = inScope;

    //Notify the observer
    if(NULL!=m_pxScubserver)
        m_pxScubserver->onScopeEntry(*m_pxCurrentScope, *pxOldScope);

}

void Scuba::exitScope(void)
{
    Scope* pxOldScope;

    pxOldScope = m_apxScope.back();
    m_apxScope.pop_back();
    m_pxCurrentScope = m_apxScope.back();

    if(NULL!=m_pxScubserver)
        m_pxScubserver->onScopeExit(*m_pxCurrentScope, *pxOldScope);
}

void Scuba::feed(Token& inToken)
{
    bool bIsEndOfScope=false;
    Scope* pxScope = NULL;

    if(NULL!=m_pxScubserver)
        m_pxScubserver->onNewToken(inToken);

    pxScope = m_pxCurrentScope->process(inToken, bIsEndOfScope);
    if(true==bIsEndOfScope)
        exitScope();
    
    if(NULL!=pxScope)
        enterScope(pxScope);

}

void Scuba::dump()
{
    m_pxCurrentScope->dump();
}

void Scuba::setObserver(Scubserver* inScubserver)
{
    m_pxScubserver = inScubserver;
	inScubserver->setScope(*m_pxCurrentScope);
}


